1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class PlayerController : MonoBehaviour {
6
7 public float speed;
8 public GameObject explosion;
9
10 private Vector3 position, realPos;
11 private float maxLeft, maxRight;
12 private AudioSource audioSource;
13
14
15 void Awake(){
16 InitializePlayerVariables ();
17 }
18
19 // Use this for initialization
20 void Start () {
21 if(GameController.instance != null && MusicController.instance != null){
22 if (GameController.instance.isMusicOn) {
23 audioSource.Play ();
24 } else {
25 audioSource.Stop ();
26 }
27 }
28 }
29
30 // Update is called once per frame
31 void Update () {
32 if (GameplayController.instance.gameInProgress) {
33 LimitPosition ();
34 if (Application.platform == RuntimePlatform.WindowsEditor) {
35 PlayerMovement ();
36 }else if(Application.platform == RuntimePlatform.Android){
37 TouchMovement ();
38 }
39
40 }
41 }
42
43 void PlayerMovement(){
44 if(Input.GetKey(KeyCode.LeftArrow)){
45 position.x -= speed * Time.deltaTime;
46 }else if(Input.GetKey(KeyCode.RightArrow)){
47 position.x += speed * Time.deltaTime;
48 }
49
50 position.x = Mathf.Clamp (position.x, maxLeft + 0.5f, maxRight - 0.5f);
51 transform.position = position;
52
53 }
54
55 void TouchMovement(){
56 if(Input.touchCount > 0){
57 Touch touch = Input.GetTouch (0);
58
59 if(touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Began){
60 Vector3 fingerPos = touch.position;
61 position.x = fingerPos.x;
62 realPos = Camera.main.ScreenToWorldPoint (new Vector3(position.x, position.y ,position.z));
63 transform.position = Vector3.Lerp (transform.position, new Vector3(Mathf.Clamp(realPos.x, maxLeft + 0.5f, maxRight - 0.5f), transform.position.y, transform.position.z), Time.deltaTime * speed);
64
65 }
66
67 if (touch.phase == TouchPhase.Moved) {
68 Vector3 fingerPos = touch.position;
69 position.x = fingerPos.x;
70 realPos = Camera.main.ScreenToWorldPoint (new Vector3(position.x, position.y ,position.z));
71 transform.position = Vector3.Lerp (transform.position, new Vector3(Mathf.Clamp(realPos.x, maxLeft + 0.5f, maxRight - 0.5f), transform.position.y, transform.position.z), speed);
72 }
73
74 }
75 }
76
77 void LimitPosition(){
78 Vector3 leftBound = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, Camera.main.transform.position.z));
79 Vector3 rightBound = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, Camera.main.transform.position.z));
80
81 maxLeft = leftBound.x;
82 maxRight = rightBound.x;
83 }
84
85 void InitializePlayerVariables(){
86 position = transform.position;
87 audioSource = GetComponent<AudioSource> ();
88 }
89
90
91 public void PlayerDied(){
92 if(GameController.instance != null && MusicController.instance != null){
93 if(GameController.instance.isMusicOn){
94 MusicController.instance.PlayerDeath ();
95 }
96 }
97 Instantiate (explosion, transform.position, Quaternion.identity);
98 Destroy (gameObject);
99 GameplayController.instance.GameOver ();
100 }
101
102 }
Use this for initialization
Update is called once per frame